package gui;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

/**
 * @author dotekien
 *         Date: Apr 4, 2006
 *         Time: 8:52:26 PM
 */
public class SameCellRenderer extends DefaultTableCellRenderer {
    Class colType;

    public SameCellRenderer(Class columnType) {
        colType = columnType;
    }

    public Component getTableCellRendererComponent(JTable table,
                                                   Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        // Be a little paranoid about where the user tries to use this renderer.
        Component c = super.getTableCellRendererComponent(table, value, isSelected,
                hasFocus, row, col);
        if (c instanceof JLabel) {
            JLabel l = (JLabel) c;
            if (colType.equals(Number.class)) {
                l.setHorizontalAlignment(SwingConstants.CENTER);
                l.setHorizontalTextPosition(SwingConstants.CENTER);
            } else {
                l.setHorizontalAlignment(SwingConstants.LEFT);
                l.setHorizontalTextPosition(SwingConstants.LEFT);
            }
        }
        return c;

    }
}
